#include "main.h"
#include "common_api.h"
#include "luat_rtos.h"
#include "luat_mem.h"
#include "luat_debug.h"
#include "usr_mqtt.h"


 uint32_t normal_msg_id = 0;

/// @brief 心跳包 一分钟一个，开锁到关锁期间可以不发心跳包
/// @return
// int heart_pkg_func()
int heart_pkg_func()
{
    int ret = -1;
    int Keepalive=180;
    char topic[32]={0};
    cJSON *send_root = cJSON_CreateObject();
    if (NULL == send_root)
    {
        return -1;
    }
   // cJSON_AddStringToObject(send_root, "DeviceID", get_4g_imei(&g_dev_lte));
    cJSON_AddNumberToObject(send_root, "Keepalive", Keepalive);
    cJSON_AddNumberToObject(send_root, "utc", get_gnss_ts(&g_ebike_status.gnss));
    char *p_tmp = cJSON_PrintUnformatted(send_root);   
    usr_mqtt_pub_msg(p_tmp, pub_topic_ping);
    cJSON_Delete(send_root);
    return ret;
}
int on_line_func(void)//设备上线数据包
{
    int ret = -1;
    cJSON *send_root = cJSON_CreateObject();
    if (NULL == send_root)
    {
        LUAT_DEBUG_PRINT("root create fail!\n");
        return -1;
    }
    cJSON_AddStringToObject(send_root, "uid", get_4g_imei(&g_dev_lte));
    cJSON_AddStringToObject(send_root, "ver4g", get_ver_4g(g_dev_info));
    cJSON_AddStringToObject(send_root, "verhd", get_ver_hard(g_dev_info));
    cJSON_AddNumberToObject(send_root, "mid", normal_msg_id++);
    cJSON *body = cJSON_CreateObject();
    if (NULL == body)
    {
        LUAT_DEBUG_PRINT("body create fail!\n");
        return -1;
    }
    cJSON_AddItemToObject(send_root, "body", body);
    // lte
    cJSON *lte = cJSON_CreateObject();
    if (NULL == lte)
    {
        LUAT_DEBUG_PRINT("ping create fail!\n");
        return -1;
    }
    cJSON_AddItemToObject(body, "lte", lte);
    cJSON_AddStringToObject(lte, "imei", get_4g_imei(&g_dev_lte));
    cJSON_AddStringToObject(lte, "imsi", get_4g_imsi(&g_dev_lte));
    cJSON_AddStringToObject(lte, "ccid", get_4g_ccid(&g_dev_lte));
    cJSON_AddNumberToObject(lte, "rssi", get_4g_csq(&g_dev_lte));
    char *p_tmp = cJSON_PrintUnformatted(send_root);   
    usr_mqtt_pub_msg(p_tmp, pub_topic_online);
    cJSON_Delete(send_root);
    return ret;
}

/// @brief 属性上报 
/// @return
int on_reoprt_func(void)
{
    int ret = -1;

    cJSON *send_root = cJSON_CreateObject();
    if (NULL == send_root)
    {        
        return -1;
    }
    cJSON_AddStringToObject(send_root, "uid", get_4g_imei(&g_dev_lte));
    cJSON_AddNumberToObject(send_root, "mid", normal_msg_id++);
    cJSON *body = cJSON_CreateObject();
    if (NULL == body)
    {
      
        return -1;
    }
    cJSON_AddItemToObject(send_root, "body", body);
    // lte
    cJSON *lte = cJSON_CreateObject();
    if (NULL == lte)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "lte", lte);
    cJSON_AddNumberToObject(lte, "rssi", get_4g_csq(&g_dev_lte));//GSM信号强度
    // gps
    cJSON *gps = cJSON_CreateObject();
    if (NULL == gps)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "gps", gps);
    cJSON_AddNumberToObject(gps, "state",get_gnss_gps_status(&g_ebike_status.gnss));//GPS定位状态
    cJSON_AddNumberToObject(gps, "fix", get_gnss_fixMode(&g_ebike_status.gnss));//定位类型
    cJSON_AddNumberToObject(gps, "sv", get_gnss_sv(&g_ebike_status.gnss));//卫星数量
    cJSON_AddNumberToObject(gps, "lng", get_gnss_lon(&g_ebike_status.gnss));//经度
    cJSON_AddNumberToObject(gps, "lat", get_gnss_lat(&g_ebike_status.gnss));//纬度
    cJSON_AddNumberToObject(gps, "vt", get_gnss_vt(&g_ebike_status.gnss));//地面航向    
    cJSON_AddNumberToObject(gps, "spd", get_gnss_speed(&g_ebike_status.gnss));//速度
    cJSON_AddNumberToObject(gps, "mileage", get_accumulated_mileage(&g_ebike_status.gnss));//累计里程
    cJSON_AddNumberToObject(gps, "smileage", get_mileage(&g_ebike_status.gnss));//小计里程
    cJSON_AddNumberToObject(gps, "utc", get_gnss_ts(&g_ebike_status.gnss));//定位时间
    // car
    cJSON *car = cJSON_CreateObject();
    if (NULL == car)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "car", car);
    int8_t mode=0;
    mode=get_ebike_state_info(&g_ebike_status,WORK_STATUS);
    cJSON_AddNumberToObject(car, "statu", mode);
    cJSON_AddNumberToObject(car, "up", get_startup_ts(g_dev_info));
    cJSON_AddNumberToObject(car, "power_f", get_ebike_state_info(&g_ebike_status, POWER_STATUS));
    if(mode==V_SLEEP)
    {
        cJSON_AddNumberToObject(car, "sleep_f", 1);//休眠
    }
    else
    {
        cJSON_AddNumberToObject(car, "sleep_f", 0);//非休眠
    }
    cJSON_AddNumberToObject(car, "locked", g_ebike_status.bike_locked);
    // ext
    cJSON *ext = cJSON_CreateObject();
    if (NULL == ext)
    {
        LUAT_DEBUG_PRINT("ext create fail!\n");
        return -1;
    }
    cJSON_AddItemToObject(body, "ext", ext);
    cJSON_AddNumberToObject(ext, "vin", get_ebike_vin(&g_ebike_status));
    cJSON_AddNumberToObject(ext, "temp", get_ebike_temp(&g_ebike_status));
    char *p_tmp = cJSON_PrintUnformatted(send_root);
    usr_mqtt_pub_msg(p_tmp,pub_topic_onreport);    
    cJSON_Delete(send_root);
    return ret;
}
/// @brief 事件上报 
/// @return
int on_change_func(void)
{
    int ret = -1;
    cJSON *send_root = cJSON_CreateObject();
    if (NULL == send_root)
    {        
        return -1;
    }
    cJSON_AddStringToObject(send_root, "uid", get_4g_imei(&g_dev_lte));
    cJSON_AddNumberToObject(send_root, "mid", normal_msg_id++);
    cJSON_AddNumberToObject(send_root, "rules",get_ebike_rules(&g_ebike_status));
    set_ebike_rules(&g_ebike_status,NO_CHANGE);
    cJSON *body = cJSON_CreateObject();
    if (NULL == body)
    {
      
        return -1;
    }
    cJSON_AddItemToObject(send_root, "body", body);
    // gps
    cJSON *gps = cJSON_CreateObject();
    if (NULL == gps)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "gps", gps);
    cJSON_AddNumberToObject(gps, "state",get_gnss_gps_status(&g_ebike_status.gnss));//GPS定位状态
    cJSON_AddNumberToObject(gps, "fix", get_gnss_fixMode(&g_ebike_status.gnss));//定位类型
    cJSON_AddNumberToObject(gps, "sv", get_gnss_sv(&g_ebike_status.gnss));//卫星数量
    cJSON_AddNumberToObject(gps, "lng", get_gnss_lon(&g_ebike_status.gnss));//经度
    cJSON_AddNumberToObject(gps, "lat", get_gnss_lat(&g_ebike_status.gnss));//纬度
    cJSON_AddNumberToObject(gps, "vt", get_gnss_vt(&g_ebike_status.gnss));//地面航向    
    cJSON_AddNumberToObject(gps, "spd", get_gnss_speed(&g_ebike_status.gnss));//速度
    cJSON_AddNumberToObject(gps, "mileage", get_accumulated_mileage(&g_ebike_status.gnss));//累计里程
    cJSON_AddNumberToObject(gps, "smileage", get_mileage(&g_ebike_status.gnss));//小计里程
    cJSON_AddNumberToObject(gps, "utc", get_gnss_ts(&g_ebike_status.gnss));//定位时间
    // car
    cJSON *car = cJSON_CreateObject();
    if (NULL == car)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "car", car);
    cJSON_AddNumberToObject(car, "statu", get_ebike_state_info(&g_ebike_status,WORK_STATUS));
    cJSON_AddNumberToObject(car, "power_f", get_ebike_state_info(&g_ebike_status, POWER_STATUS));
    cJSON_AddNumberToObject(car, "vin", get_ebike_vin(&g_ebike_status));
    cJSON_AddNumberToObject(car, "anti_theft",get_ebike_state_info(&g_ebike_status, ANTI_THEFT));
    cJSON_AddNumberToObject(car, "gps_f",get_ebike_state_info(&g_ebike_status, GPS_STATUS));
    char *p_tmp = cJSON_PrintUnformatted(send_root);
    usr_mqtt_pub_msg(p_tmp,pub_topic_onchange);    
    cJSON_Delete(send_root);
    return ret;
}
int on_control_func(void)
{
 int ret = -1;

    cJSON *send_root = cJSON_CreateObject();
    if (NULL == send_root)
    {        
        return -1;
    }
    cJSON_AddStringToObject(send_root, "uid", get_4g_imei(&g_dev_lte));
    cJSON_AddNumberToObject(send_root, "mid", normal_msg_id++);
    cJSON_AddStringToObject(send_root, "type","r-config");
    cJSON *body = cJSON_CreateObject();
    if (NULL == body)
    {
      
        return -1;
    }
    cJSON_AddItemToObject(send_root, "body", body);
    // bsloc
    cJSON *bsloc = cJSON_CreateObject();
    if (NULL == bsloc)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "bsloc", bsloc);
    cJSON_AddStringToObject(bsloc, "iccid", get_4g_ccid(&g_dev_lte));
    cJSON_AddStringToObject(bsloc, "imsi", get_4g_imsi(&g_dev_lte));

    cJSON_AddNumberToObject(bsloc, "rssi", get_bsloc_rssi(&g_dev_bsloc));    
    cJSON_AddNumberToObject(bsloc, "rsrq", get_bsloc_rsrq(&g_dev_bsloc));
    cJSON_AddNumberToObject(bsloc, "mcc", get_bsloc_mcc(&g_dev_bsloc));    
    cJSON_AddNumberToObject(bsloc, "mnc", get_bsloc_mnc(&g_dev_bsloc));
    cJSON_AddNumberToObject(bsloc, "lac", get_bsloc_lac(&g_dev_bsloc));    
    cJSON_AddNumberToObject(bsloc, "cid", get_bsloc_cid(&g_dev_bsloc));
    // config
    cJSON *config = cJSON_CreateObject();
    if (NULL == config)
    {
        return -1;
    }
    cJSON_AddItemToObject(body, "gps", config);
    cJSON_AddNumberToObject(config, "rmt",g_dev_info->info.move_up_cycle);//
    cJSON_AddNumberToObject(config, "rst",g_dev_info->info.stop_up_cycle);//
    cJSON_AddStringToObject(config, "usn", g_dev_info->info.product_id);//
    cJSON_AddStringToObject(config, "addr", g_dev_info->info.addr);//
    cJSON_AddNumberToObject(config, "port", g_dev_info->info.port);//
    cJSON_AddNumberToObject(config, "acc",  get_ebike_state_info(&g_ebike_status, ACC_STATUS));//   电门状态
    cJSON_AddNumberToObject(config, "guard",get_ebike_state_info(&g_ebike_status, BIKE_LOCKED));//设防状态
    cJSON_AddNumberToObject(config, "qry",  get_ebike_state_info(&g_ebike_status, QRY_STATUS));// 寻车状态
    
    char *p_tmp = cJSON_PrintUnformatted(send_root);
    usr_mqtt_pub_msg(p_tmp,pub_topic_oncontrol);    
    cJSON_Delete(send_root);
    return ret;
}
int control_cmd_rsp(int mid, int ret)
{
    cJSON *control_rsp = cJSON_CreateObject();
    if (NULL == control_rsp)
    {
        LUAT_DEBUG_PRINT("control_rsp create fail!\n");
        return -1;
    }
    cJSON_AddStringToObject(control_rsp, "uid", get_4g_imei(&g_dev_lte));
    cJSON_AddStringToObject(control_rsp, "type", "w-config");
    cJSON_AddNumberToObject(control_rsp, "mid", mid);
    cJSON *body = cJSON_CreateObject();
    if (NULL == body)
    {
        LUAT_DEBUG_PRINT("body create fail!\n");
        return -1;
    }
    cJSON_AddItemToObject(control_rsp, "body", body); 
    cJSON_AddNumberToObject(body, "statu", ret);
    char *p_tmp = cJSON_PrintUnformatted(control_rsp);
    usr_mqtt_pub_msg(p_tmp,  pub_topic_oncontrol);
    cJSON_Delete(control_rsp);
    return 0;
}

uint32_t mid = 0;
uint32_t blck_mid = 0;
uint8_t enter_area_flag = 0;
int control_cmd_handle(char *buffer)
{
    int ret = 0;
    char *cmd_type;
    char *type;
    cJSON *control_cmd = cJSON_Parse(buffer);
    if (NULL == control_cmd)
    {
        //usr_log("create control_cmd erro\n");
        return -1;
    }
    type=cJSON_GetObjectItem(control_cmd,"type")->valuestring;
    mid = cJSON_GetObjectItem(control_cmd, "mid")->valueint;
    usr_log(">>>> control cmd mid: %d", mid);
    if (0==strcmp(type,"config"))//握手成功
    {
        g_dev_info->info.online_flag=false;
    }
    else if (0==strcmp(type,"report"))//获取属性报告
    {
        luat_rtos_message_send(message_event_deal_task_handle, CASE_TASK_ON_REPORT, (void *)NULL);
    }
    else if (0==strcmp(type,"r-config"))
    {
        luat_rtos_message_send(message_event_deal_task_handle, CASE_TASK_ON_CONTROL, (void *)NULL);
    }
    else if (0==strcmp(type,"w-config"))//写配置数据
    {
        cJSON *cmd_body = cJSON_GetObjectItem(control_cmd, "body");
        if (NULL == cmd_body)
        {
            usr_log("get control body erro\n");
            goto exit;
        }    
        cmd_type = cmd_body->child->string;
        usr_log(">>> cmd_body->child: %s", cmd_body->child->string);
        /// 打开acc,针对出界上下电
        if (0 == strcmp(cmd_type, "acc"))
        {
            int val = cJSON_GetObjectItem(cmd_body, "acc")->valueint;
            if (val == 1) /// 上ACC
            {
                v_acc_ctrl(1);
                v_lock_ctrl(0);                
                set_ebike_state_info(&g_ebike_status,ACC_STATUS,V_ON);
                set_ebike_state_info(&g_ebike_status,BIKE_LOCKED,V_OFF);
                control_cmd_rsp(mid, ret);
            }

            if (val == 0) /// 下ACC
            {
            if (get_ebike_state_info(&g_ebike_status, MOVE_FLAG) == V_OFF) /// 无轮动可还车
            { 
                    v_acc_ctrl(0); /// 下ACC
                    v_lock_ctrl(1); /// 设防                       
                    set_ebike_state_info(&g_ebike_status,ACC_STATUS,V_OFF);
                    set_ebike_state_info(&g_ebike_status,BIKE_LOCKED,V_ON);
                    control_cmd_rsp(mid, ret);
            }
            else
                {
                    control_cmd_rsp(mid, -1); /// 有轮动,上报还车失败
                }
            }
        }

        /// 设防、撤防
        if (0 == strcmp(cmd_type, "guard"))
        {
            int val = cJSON_GetObjectItem(cmd_body, "guard")->valueint;
            if (val == 0) /// 撤防
            {
                v_acc_ctrl(1);
                v_lock_ctrl(0);
                set_ebike_state_info(&g_ebike_status,ACC_STATUS,V_ON);
                set_ebike_state_info(&g_ebike_status,BIKE_LOCKED,V_OFF);
                control_cmd_rsp(mid, ret);
            }

            if (val == 1) ///设防
            {
                if (get_ebike_state_info(&g_ebike_status, MOVE_FLAG) == V_OFF) /// 无轮动可还车
                { 
                        v_acc_ctrl(0); /// 下ACC
                        v_lock_ctrl(1); /// 设防         
                        set_ebike_state_info(&g_ebike_status,ACC_STATUS,V_OFF);
                        set_ebike_state_info(&g_ebike_status,BIKE_LOCKED,V_ON);         
                        control_cmd_rsp(mid, ret);
                }
                else
                {
                    control_cmd_rsp(mid, -1); /// 有轮动,上报还车失败
                }
            }
        }
            /// 寻车
        if (0 == strcmp(cmd_type, "qry"))
        {
            int val = cJSON_GetObjectItem(cmd_body, "qry")->valueint;
            if (val == 1) /// 寻车，蜂鸣器响五次
            {
                beep_player.play_num=5;
                set_ebike_state_info(&g_ebike_status,QRY_STATUS,V_ON);
                control_cmd_rsp(mid, ret);
            }

            else if(val == 0) /// 关闭寻车
            {
                beep_player.play_num=0;
                set_ebike_state_info(&g_ebike_status,QRY_STATUS,V_OFF);
                control_cmd_rsp(mid, ret);         
            }       
        }
            /// 
        if (0 == strcmp(cmd_type, "rmt"))
        {
            g_dev_info->info.move_up_cycle=cJSON_GetObjectItem(cmd_body, "rmt")->valueint;        
            control_cmd_rsp( mid, ret);                    
        }
        if (0 == strcmp(cmd_type, "rst"))
        {
            g_dev_info->info.stop_up_cycle=cJSON_GetObjectItem(cmd_body, "rst")->valueint;
            control_cmd_rsp(mid, ret);
        }
        if (0 == strcmp(cmd_type, "usn"))
        {
            sprintf(g_dev_info->info.product_id,"%s",cJSON_GetObjectItem(cmd_body, "usn")->valuestring);
            control_cmd_rsp(mid, ret);
        }
        if (0 == strcmp(cmd_type, "addr"))
        {
            sprintf(g_dev_info->info.addr,"%s",cJSON_GetObjectItem(cmd_body, "addr")->valuestring);
            control_cmd_rsp(mid, ret);
        }
        if (0 == strcmp(cmd_type, "port"))
        {
            g_dev_info->info.port=cJSON_GetObjectItem(cmd_body, "port")->valueint;
            control_cmd_rsp( mid, ret);
        }
    }
    
    
    
    
    
       /*
     * 进入低功耗
     */
    // if (0 == strcmp(cmd_type, "sleep.mode"))
    // {
    //     usr_log("go to sleep.mode");
    //     if ((g_ebike_status.acc_statu == V_OFF)&& (luat_pm_check() == LUAT_PM_SLEEP_MODE_NONE))
    //     {      
    //         set_ebike_state_info(&g_ebike_status,WORK_STATUS,V_SLEEP);//强制进入低功耗状态       
    //         usr_enter_low_power();//
    //     }        
    // }
    

    // /// 远程复位
    // if (0 == strcmp(cmd_type, "hw.rst"))
    // {
    //     cJSON *tmp = cJSON_GetObjectItem(cmd_body, "hw.rst");
    //     if (NULL == tmp)
    //     {
    //         goto exit;
    //     }
    //     control_cmd_rsp("hw.rst", mid, ret);
    //     luat_os_reboot(0);
    // }

    exit: 
        cJSON_Delete(control_cmd);
        return ret;
}

// int pub_ota_state(uint8_t module, uint32_t pkt_total, uint32_t pkt_num, uint8_t state)
// {
//     cJSON *ota_rsp = cJSON_CreateObject();
//     if (NULL == ota_rsp)
//     {
//         usr_log("ota_rsp create fail!\n");
//         return -1;
//     }
//     cJSON_AddNumberToObject(ota_rsp, "module", module);
//     cJSON_AddNumberToObject(ota_rsp, "pktTotal", pkt_total);
//     cJSON_AddNumberToObject(ota_rsp, "pktNum", pkt_num);
//     cJSON_AddNumberToObject(ota_rsp, "state", state);

//     char *p_tmp = cJSON_PrintUnformatted(ota_rsp);

//     usr_mqtt_pub_msg(p_tmp, pub_topic_ota_rsp);

//     //free(p_tmp);
//     cJSON_Delete(ota_rsp);
//     return 0;
// }

// int config_cmd_rsp(char *cmd, int mid, int ret)
// {
//     cJSON *control_rsp = cJSON_CreateObject();
//     if (NULL == control_rsp)
//     {
//         LUAT_DEBUG_PRINT("control_rsp create fail!\n");
//         return -1;
//     }
//     cJSON_AddStringToObject(control_rsp, "type", "cfg_rsp");
//     cJSON_AddNumberToObject(control_rsp, "mid", mid);
//     cJSON_AddNumberToObject(control_rsp, "ts", get_real_ts(g_dev_info));

//     cJSON *body = cJSON_CreateObject();
//     if (NULL == body)
//     {
//         LUAT_DEBUG_PRINT("body create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(control_rsp, "body", body);

//     cJSON *body_cmd = cJSON_CreateObject();
//     if (NULL == body_cmd)
//     {
//         LUAT_DEBUG_PRINT("body create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(body, cmd, body_cmd);
//     cJSON_AddNumberToObject(body_cmd, "seq", mid);
//     cJSON_AddNumberToObject(body_cmd, "code", ret);

//     char *p_tmp = cJSON_PrintUnformatted(control_rsp);
//     LUAT_DEBUG_PRINT(">>>>>>> %s", p_tmp);

//     usr_mqtt_pub_msg(p_tmp, pub_topic_config_rsp);

//     //free(p_tmp);
//     cJSON_Delete(control_rsp);
//     return 0;
// }

// int version_cmd_rsp(char *cmd, int mid, int ret)
// {
//     cJSON *version_rsp = cJSON_CreateObject();
//     if (NULL == version_rsp)
//     {
//         LUAT_DEBUG_PRINT("version_rsp create fail!\n");
//         return -1;
//     }
//     cJSON_AddStringToObject(version_rsp, "ver4g", get_ver_4g(g_dev_info));
//     cJSON_AddStringToObject(version_rsp, "verjl", get_ver_ble(g_dev_info));
//     cJSON_AddStringToObject(version_rsp, "verhd", get_ver_hard(g_dev_info));
//     cJSON_AddStringToObject(version_rsp, "type", "ctl-cmd");

//     cJSON *body = cJSON_CreateObject();
//     if (NULL == body)
//     {
//         LUAT_DEBUG_PRINT("body create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(version_rsp, "body", body);

//     cJSON *body_cmd = cJSON_CreateObject();
//     if (NULL == body_cmd)
//     {
//         LUAT_DEBUG_PRINT("body create fail!\n");
//         return -1;
//     }

//     cJSON_AddItemToObject(body, cmd, body_cmd);
//     cJSON_AddNumberToObject(body_cmd, "seq", mid);
//     cJSON_AddNumberToObject(body_cmd, "code", ret);

//     char *p_tmp = cJSON_PrintUnformatted(version_rsp);
//     LUAT_DEBUG_PRINT(">>>>>>> %s", p_tmp);

//     usr_mqtt_pub_msg(p_tmp, pub_topic_control_rsp);

//     cJSON_Delete(version_rsp);
//     return 0;
// }

// int config_cmd_handle(char *buffer)
// {
//     uint32_t mid = 0;
//     int ret=0;

//     char *cmd_type;

//     cJSON *config_cmd = cJSON_Parse(buffer);
//     if (NULL == config_cmd)
//     {
//         LUAT_DEBUG_PRINT("create config erro\n");
//         return -1;
//     }
//     mid = cJSON_GetObjectItem(config_cmd, "mid")->valueint;
//     LUAT_DEBUG_PRINT(">>>> config cmd mid: %d", mid);

//     cJSON *cmd_body = cJSON_GetObjectItem(config_cmd, "body");
//     if (NULL == cmd_body)
//     {
//         LUAT_DEBUG_PRINT("get body erro\n");
//         ret = -1;
//         goto exit;
//     }
//     cmd_type = cmd_body->child->string;

//     if (0 == strcmp(cmd_type, "mqtt.svr"))
//     {
//         cJSON *mqtt_ser = cJSON_GetObjectItem(cmd_body, "mqtt.svr");
//         if (NULL == mqtt_ser)
//         {
//              return -1;
//         }

//         if(get_ebike_vin(&g_ebike_status) <= 48000)
//         {
//             config_cmd_rsp("mqtt.svr", mid, -1);
//             goto exit;
//         }

//         usr_log(">>>>> %s", cJSON_GetObjectItem(mqtt_ser, "addr")->valuestring);

//         config_cmd_rsp("mqtt.svr", mid, 0);
//         usr_mqtt_switch_server(
//             cJSON_GetObjectItem(mqtt_ser, "addr")->valuestring,
//             cJSON_GetObjectItem(mqtt_ser, "port")->valuestring,
//             cJSON_GetObjectItem(mqtt_ser, "username")->valuestring,
//             cJSON_GetObjectItem(mqtt_ser, "passwd")->valuestring);
        
//     }
    
//     if (0 == strcmp(cmd_type, "rtk.svr"))
//     {
//         cJSON *rtk_svr = cJSON_GetObjectItem(cmd_body, "rtk.svr");
//         if (NULL == rtk_svr)
//         {
//             goto exit;
//         }

//         if(get_ebike_vin(&g_ebike_status) <= 48000)
//         {
//             config_cmd_rsp("rtk.svr", mid, -1);
//             goto exit;
//         }

//         config_cmd_rsp("rtk.svr", mid, 0);
//         // usr_rtk_switch(
//         //     cJSON_GetObjectItem(rtk_svr, "addr")->valuestring,
//         //     cJSON_GetObjectItem(rtk_svr, "point")->valuestring,
//         //     cJSON_GetObjectItem(rtk_svr, "port")->valueint,
//         //     cJSON_GetObjectItem(rtk_svr, "username")->valuestring,
//         //     cJSON_GetObjectItem(rtk_svr, "userpswd")->valuestring);
//         usr_rtk_clear();
//     }

//     exit:
//         cJSON_Delete(config_cmd);
//         return ret;
// }

// int all_status_reoprt_func()
// {
//     int ret = -1;

//     cJSON *send_root = cJSON_CreateObject();
//     if (NULL == send_root)
//     {
//         LUAT_DEBUG_PRINT("root create fail!\n");
//         return -1;
//     }
//     cJSON_AddStringToObject(send_root, "ver4g", get_ver_4g(g_dev_info));
//     cJSON_AddStringToObject(send_root, "verjl", get_ver_ble(g_dev_info));
//     cJSON_AddStringToObject(send_root, "verhd", get_ver_hard(g_dev_info));
//     cJSON_AddStringToObject(send_root, "verad", get_ver_audio(g_dev_info));
//     cJSON_AddNumberToObject(send_root, "sTime", get_startup_ts(g_dev_info));
//     cJSON_AddNumberToObject(send_root, "sReason", get_startup_reason(g_dev_info));
//     cJSON_AddNumberToObject(send_root, "audio", get_dev_info_audio_exist(g_dev_info));
//     cJSON_AddStringToObject(send_root, "type", "st-rpt");
//     cJSON_AddNumberToObject(send_root, "mid", normal_msg_id++);
//     cJSON_AddNumberToObject(send_root, "ts", get_real_ts(g_dev_info));
//     cJSON_AddNumberToObject(send_root, "ver", 1);

//     cJSON *body = cJSON_CreateObject();
//     if (NULL == body)
//     {
//         LUAT_DEBUG_PRINT("body create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(send_root, "body", body);


//     // lte
//     cJSON *lte = cJSON_CreateObject();
//     if (NULL == lte)
//     {
//         LUAT_DEBUG_PRINT("ping create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(body, "lte", lte);
//     cJSON_AddStringToObject(lte, "imei", get_4g_imei(&g_dev_lte));
//     cJSON_AddStringToObject(lte, "imsi", get_4g_imsi(&g_dev_lte));
//     cJSON_AddStringToObject(lte, "ccid", get_4g_ccid(&g_dev_lte));
//     cJSON_AddNumberToObject(lte, "csq", get_4g_csq(&g_dev_lte));

//     // gps
//     cJSON *gps = cJSON_CreateObject();
//     if (NULL == gps)
//     {
//         LUAT_DEBUG_PRINT("ping create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(body, "gps", gps);
//     cJSON_AddNumberToObject(gps, "fix", get_gnss_fixMode(&g_ebike_status.gnss));
//     cJSON_AddNumberToObject(gps, "sv", get_gnss_sv(&g_ebike_status.gnss));
//     cJSON_AddNumberToObject(gps, "lat", get_gnss_lat(&g_ebike_status.gnss));
//     cJSON_AddStringToObject(gps, "ns", get_gnss_ns(&g_ebike_status.gnss));
//     cJSON_AddNumberToObject(gps, "lng", get_gnss_lon(&g_ebike_status.gnss));
//     cJSON_AddStringToObject(gps, "ew", get_gnss_ew(&g_ebike_status.gnss));
//     cJSON_AddNumberToObject(gps, "spd", get_gnss_speed(&g_ebike_status.gnss));
//     cJSON_AddNumberToObject(gps, "utc", get_gnss_ts(&g_ebike_status.gnss));
//     // bsloc
//     cJSON *bsloc = cJSON_CreateObject();
//     if (NULL == bsloc)
//     {
//         LUAT_DEBUG_PRINT("ping create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(body, "bsl", bsloc);
//     cJSON_AddNumberToObject(bsloc, "csq", get_4g_csq(&g_dev_lte));
//     cJSON_AddNumberToObject(bsloc, "lat", get_bsloc_lat(&g_ebike_status.bsloc));
//     cJSON_AddNumberToObject(bsloc, "lng", get_bsloc_lon(&g_ebike_status.bsloc));
//     cJSON_AddNumberToObject(bsloc, "utc", get_bsloc_ts(&g_ebike_status.bsloc));
//     cJSON_AddNumberToObject(bsloc, "state", get_bsloc_state(&g_ebike_status.bsloc));

//     // bat
//     cJSON *bat = cJSON_CreateObject();
//     if (NULL == bat)
//     {
//         LUAT_DEBUG_PRINT("bat create fail!\n");
//         return -1;
//     }
//     cJSON_AddItemToObject(body, "bat", bat);
//     cJSON_AddNumberToObject(bat, "vol", g_jl_info.bat.vol);
//     cJSON_AddNumberToObject(bat, "temp", g_jl_info.bat.ntc_temp);
//     cJSON_AddNumberToObject(bat, "state", g_jl_info.bat.charge_state);

//     char *p_tmp = cJSON_PrintUnformatted(send_root);
//     usr_mqtt_pub_msg(p_tmp, pub_topic_status);

//     cJSON_Delete(send_root);
//     return ret;
// }

/*
 *mqtt协议解析处理
 */
void mqtt_msg_handler(char *topic, uint16_t topic_len, char *payload)
{
    // usr_log("topic: %.*s", topic_len, topic);
    // usr_log("msg: %s", payload);

    clear_sleep_count(&g_ebike_status.sleep);
    if(get_ebike_state_info(&g_ebike_status, WORK_STATUS) == V_SLEEP)
    {
        usr_quit_low_power();
        set_ebike_state_info(&g_ebike_status,WORK_STATUS,V_STOP);//退出低功耗状态
    }

    if (!strncmp(topic, sub_topic_control, topic_len))
    {
       // usr_log("topic: control");        
        data_mode_count_add(&gsm_work_led);
        control_cmd_handle(payload);
    }
    
}
